package com.jgssp.ad.sdk.mediation.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.splash.MediationCustomSplashLoader;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig;
import com.jgssp.ad.sdk.mediation.utils.PriceUtil;
import com.jgssp.ad.sdk.mediation.utils.ThreadUtils;

import java.util.Map;

import cn.jiguang.jgssp.ad.ADJgSplashAd;
import cn.jiguang.jgssp.ad.data.ADJgAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgSplashAdListener;

public class CustomerSplashAdapter extends MediationCustomSplashLoader {

    private ADJgSplashAd splashAd;
    private ADJgAdInfo splashAdInfo;

    @Override
    public void load(final Context context, AdSlot adSlot, final MediationCustomServiceConfig serviceConfig) {


        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {

                if (!(context instanceof Activity)) {
                    callLoadFail(-1, "JgSSP Context must be Activity");
                    return;
                }

                splashAd = new ADJgSplashAd((Activity) context);
                splashAd.setImmersive(true);
                splashAd.setListener(new ADJgSplashAdListener() {
                    @Override
                    public void onADTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onReward(ADJgAdInfo adJgAdInfo) {

                    }

                    @Override
                    public void onAdReceive(ADJgAdInfo adInfo) {
                        splashAdInfo = adInfo;
                        if (isClientBidding() && adInfo != null) {//bidding广告类型
                            //bidding广告成功回调，回传竞价广告价格 单位：分
                            callLoadSuccess(PriceUtil.toCent(adInfo.getECPM()));
                        } else {
                            //普通广告成功回调
                            callLoadSuccess();
                        }
                    }

                    @Override
                    public void onAdExpose(ADJgAdInfo adInfo) {
                        callSplashAdShow();
                    }

                    @Override
                    public void onAdClick(ADJgAdInfo adInfo) {
                        callSplashAdClicked();
                    }

                    @Override
                    public void onAdSkip(ADJgAdInfo adInfo) {
                        callSplashAdSkip();
                    }

                    @Override
                    public void onAdClose(ADJgAdInfo adInfo) {
                        callSplashAdDismiss();
                    }

                    @Override
                    public void onAdFailed(ADJgError error) {
                        callLoadFail(error.getCode(), error.getError());
                    }
                });
                if (serviceConfig != null) {
                    splashAd.loadOnly(serviceConfig.getADNNetworkSlotId());
                }
            }
        });

    }

    @Override
    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (splashAdInfo != null && !splashAdInfo.isReleased()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    /**
     * show在主线程中回调
     */
    @Override
    public void showAd(final ViewGroup viewGroup) {
        Log.i(Const.TAG, "showAd");
        if (splashAdInfo != null && viewGroup != null) {
            splashAd.showSplash(viewGroup);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (splashAd != null) {
            splashAd.release();
            splashAd = null;
        }
        if (splashAdInfo != null) {
            splashAdInfo.release();
            splashAdInfo = null;
        }
    }

    @Override
    public void receiveBidResult(boolean win, double winnerPrice, int loseReason, Map<String, Object> extra) {
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return getBiddingType() == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }

}
